/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package openaccessagrovoctagger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author root
 */
public class GetFidFromUpload {

    public int getFid(int nid)
    {

        int fid = 0;
        String description = "";

        Connection conn = null;
        Statement stmt_fid = null;
        ResultSet rs_fid = null;


        try {

            /*  Register the driver
		The following Class.forName() statement is not necessary if using Java SE 6 (1.6) or later */

//            System.out.println("Haribol check!!!!!!!!  1");
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            try {

//                System.out.println("Haribol check!!!!!!!!  2");
		/* Get a connection to the database */
                conn = DriverManager.getConnection(OpenAccessAgrovocTagger.dbPath);

//                System.out.println("Haribol check!!!!!!!!  3");

                try {

//                    System.out.println("Haribol check!!!!!!!!  4");
                    stmt_fid = conn.createStatement();

                    String FID="";

                    String type = "";

//                    System.out.println("SELECT fid, description FROM upload where nid=" + nid + " order by fid");


//                    nid = 3347;
		    /* Execute the query */
//                    rs_nid = stmt_nid.executeQuery("SELECT nid FROM node where type='open_access' and nid>3340 order by nid");
                    rs_fid = stmt_fid.executeQuery("SELECT fid, description FROM upload where nid=" + nid + " order by fid");

                    /* The following 3 lines are for finding out the size of the result set */
                    rs_fid.last();
                    int rowCount = rs_fid.getRow();
                    rs_fid.beforeFirst();

                    System.out.println("Retrieved in getFid " + rowCount + " row(s).\n");



                                        /* Retrieve the data from the result set */
                    while (rs_fid.next()) {
                        fid = rs_fid.getInt("fid");
                        description = rs_fid.getString("description");
                        System.out.println("fid:: " + fid);
                        System.out.println("Description : " + description);


                    }                 

                    //File file = new File("/var/lib/mysql/agropedia_fortest/");

                }
                catch (Exception ex)
                {
                    System.out.println("Exception here :" + ex);
                }

                finally {
                    /* Release the resources */
                    if (rs_fid != null) {
                        try {
                            rs_fid.close();
                        } catch (SQLException sqlEx) {
                               System.out.println("SQLException: " + sqlEx.getMessage());
                        }
                        rs_fid = null;
                    }

                    if (stmt_fid != null) {
                        try {
                            stmt_fid.close();
                        } catch (SQLException sqlEx) {
                               System.out.println("SQLException: " + sqlEx.getMessage());
                        }

                        stmt_fid = null;
                    }

                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException sqlEx) {
                               // Ignore
                        }

                        conn = null;
                    }

                }

            } catch (SQLException ex) {
//                System.out.println("SQLException HariBol Check: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            }

        } catch (Exception ex) {
            System.out.println("Exception: " + ex.getMessage());
        }

        return fid;

    }

}
